#include <gtk/gtk.h>

#include "common.h"

/* GtkButton */
static void button_demo(GtkFixed *fixed);
/* GtkCheckbutton */
static void checkButton_demo(GtkFixed *fixed, gpointer window);
/* GtkFrame */
static void frame_demo(GtkFixed *fixed);
/* GtkLabel */
static void label_demo(GtkFixed *fixed);
/* GtkLabel markup */
static void label_markup_demo(GtkFixed *fixed);
/* GtkComboBox 下拉框*/
static void comboBox_demo(GtkFixed *fixed);
/* GtkHSeparator 水平分隔符*/
static void hSep_demo(GtkFixed *fixed);
/* GtkEntry 单选文本输入框 */
static void entry_demo(GtkFixed *fixed);
/* GtkImage 显示图像 */
static void image_demo(GtkFixed *fixed);
/* GtkStatusbar 状态栏 */
static void statusbar_demo(GtkBox *vbox, GtkFixed *fixed);

/* 槽 */
void on_checkButton_clicked(GtkWidget *widget, gpointer window);
void on_comboBox_selected(GtkWidget *widget, gpointer label);
void on_button_clicked(GtkWidget *widget, gpointer statusbar);

static gint x = 10;
static gint y = 10;

int main( int argc, char *argv[]){
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *fixed;
    GdkColor  color_black_ = {0x0, 0x50, 0x50, 0x50};

    gtk_init(&argc, &argv);

    /* window */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
#if 0
    gtk_window_fullscreen(GTK_WINDOW(window));              /*设置全屏*/
#else
    gtk_widget_set_size_request (window, 750, 550);         /*设置窗口大小*/
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE);    /*设置窗口不能拉伸*/
#endif
    gtk_window_set_title(GTK_WINDOW(window), TITLE);
//    gtk_container_set_border_width(GTK_CONTAINER(window), 15);  /*设置容器边界大小*/
//    gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color_black_); /*设置为黑色背景*/
    gtk_window_set_decorated(GTK_WINDOW(window), FALSE);	/*设置不提供装饰*/

    /* vbox */
    vbox = gtk_vbox_new(FALSE, 2);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    /* fixed */
    fixed = gtk_fixed_new();
    gtk_box_pack_start(GTK_BOX(vbox), fixed, FALSE, FALSE, 1);

    /* demo */
    button_demo(GTK_FIXED(fixed));
//    checkButton_demo(GTK_FIXED(fixed), (gpointer)window);
//    frame_demo(GTK_FIXED(fixed));
//    label_demo(GTK_FIXED(fixed));
    label_markup_demo(GTK_FIXED(fixed));
    comboBox_demo(GTK_FIXED(fixed));
    hSep_demo(GTK_FIXED(fixed));
    entry_demo(GTK_FIXED(fixed));
    image_demo(GTK_FIXED(fixed));
    statusbar_demo(GTK_BOX(vbox), GTK_FIXED(fixed));

    /* signal setting */
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

void on_button_clicked(GtkWidget *widget, gpointer statusbar)
{
    gchar *str;
    GtkWidget *w;
    str = g_strdup_printf("Button %s clicked", gtk_button_get_label(GTK_BUTTON(widget)));
    gtk_statusbar_push(GTK_STATUSBAR(statusbar),
                       gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), str),
                       str);
    g_free(str);
}

void statusbar_demo(GtkBox *vbox, GtkFixed *fixed)
{
    GtkWidget *button1;
    GtkWidget *button2;
    GtkWidget *statusbar;

    /* button1 */
    button1 = gtk_button_new_with_label("OK");
    gtk_widget_set_size_request(button1, 80, 30);

    /* button2 */
    button2 = gtk_button_new_with_label("Apply");
    gtk_widget_set_size_request(button2, 80, 30);

    gtk_fixed_put(fixed, button1, x, y);
    gtk_fixed_put(fixed, button2, x+100, y);
    y += 60;

    statusbar = gtk_statusbar_new();
    gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, TRUE, 1);

    g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK(on_button_clicked), (gpointer)statusbar);
    g_signal_connect(G_OBJECT(button2), "clicked", G_CALLBACK(on_button_clicked), (gpointer)statusbar);
}

void image_demo(GtkFixed *fixed)
{
    GtkWidget *image;

    image = gtk_image_new_from_file("battery.png");
    gtk_fixed_put(fixed, image, 500, 0);
}

void entry_demo(GtkFixed *fixed)
{
    GtkWidget *nameLabel;
    GtkWidget *ageLabel;
    GtkWidget *occupationLabel;
    GtkWidget *nameEntry;
    GtkWidget *ageEntry;
    GtkWidget *occupationEntry;
    GtkWidget *table;

    nameLabel = gtk_label_new("Name");
    ageLabel = gtk_label_new("Age");
    occupationLabel = gtk_label_new("Occupation");
    nameEntry = gtk_entry_new();
    ageEntry = gtk_entry_new();;
    occupationEntry = gtk_entry_new();

    table = gtk_table_new(3, 2, FALSE);
    gtk_table_attach(GTK_TABLE(table), nameLabel, 0, 1, 0, 1, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 5, 5);
    gtk_table_attach(GTK_TABLE(table), ageLabel, 0, 1, 1, 2, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 5, 5);
    gtk_table_attach(GTK_TABLE(table), occupationLabel, 0, 1, 2, 3, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 5, 5);
    gtk_table_attach(GTK_TABLE(table), nameEntry, 1, 2, 0, 1, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 5, 5);
    gtk_table_attach(GTK_TABLE(table), ageEntry, 1, 2, 1, 2, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 5, 5);
    gtk_table_attach(GTK_TABLE(table), occupationEntry, 1, 2, 2, 3, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 5, 5);

    gtk_fixed_put(fixed, table, x, y);

    y += 40 * 3;
}

void hSep_demo(GtkFixed *fixed)
{
    GtkWidget *label1, *label2;
    GtkWidget *hsep;
    GtkWidget *vbox;

    label1 = gtk_label_new("Zinc is a moderately reactive, blue gray metal that tarnishes in moist air and burns in air with a bright bluish-green flame,giving off fumes of zinc oxide. It reacts with acids, alkalis and other non-metals.If not completely pure, zinc reacts with dilute acids to release hydrogen.");
    gtk_label_set_line_wrap(GTK_LABEL(label1), TRUE);   /*设置内容自动换行*/

    label2 = gtk_label_new("Copper is an essential trace nutrient to all high plants and animals. In animals, including humans, it is found primarily in the bloodstream, as a co-factor in various enzymes, and in copper-based pigments. However, in sufficient amounts, copper can be poisonous and even fatal to organisms.");
    gtk_label_set_line_wrap(GTK_LABEL(label2), TRUE);

    vbox = gtk_vbox_new(FALSE, 10);
    gtk_fixed_put(fixed, vbox, x, y);

    hsep = gtk_hseparator_new();
    gtk_box_pack_start(GTK_BOX(vbox), label1, FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), hsep, FALSE, TRUE, 10);
    gtk_box_pack_start(GTK_BOX(vbox), label2, FALSE, TRUE, 0);
    y += 18 * 12;
}

void on_comboBox_selected(GtkWidget *widget, gpointer label)
{
    gchar *text = gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
    gtk_label_set_text(GTK_LABEL(label), text);
    g_free(text);
}

void comboBox_demo(GtkFixed *fixed)
{
    GtkWidget *comboBox;
    GtkWidget *label;

    comboBox = gtk_combo_box_new_text();
    gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "Ubuntu");
    gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "Mandriva");
    gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "Fedora");
    gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "Mint");
    gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "Gentoo");
    gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "Debian");
//    gtk_combo_box_set_active(GTK_COMBO_BOX(comboBox), 0);
    gtk_fixed_put(fixed, comboBox, x, y);

    label = gtk_label_new("-");
    gtk_fixed_put(fixed, label, x, y+40);

    y += 60;

    g_signal_connect(G_OBJECT(comboBox), "changed", G_CALLBACK(on_comboBox_selected), (gpointer)label);
}

void label_markup_demo(GtkFixed *fixed)
{
    GtkWidget *label;
    char *str = "<b>JakeCode</b>, knowledge only matters";

    label = gtk_label_new(NULL);

    gtk_label_set_markup(GTK_LABEL(label), str);
    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);

    gtk_fixed_put(fixed, label, x, y);
    y += 17;
}

void label_demo(GtkFixed *fixed)
{
    GtkWidget *label;
    label = gtk_label_new("Cold was my soul\nUntold was the pain\nI faced "
                          "when you left me\nA rose in the rain....\nSo I swore to the razor\nThat "\
                          "never, enchained\nWould your dark nails of faith\nBe pushed through my "\
                          "veins again\n\nBared on your tomb\nI'm a prayer for your "\
                          "loneliness\nAnd would you ever soon\nCome above onto me?\nFor once "\
                          "upon a time\nOn the binds of your lowliness\nI could always find the "\
                          "slot for your sacred key");
    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);    /*文字居中*/
    gtk_fixed_put(fixed, label, x, y);
    y += 17 * 16;
}

void frame_demo(GtkFixed *fixed)
{
    GtkWidget *frame[4];
    int i;

    frame[0] = gtk_frame_new("Shadow In");
    gtk_frame_set_shadow_type(GTK_FRAME(frame[0]), GTK_SHADOW_IN);

    frame[1] = gtk_frame_new("Shadow Out");
    gtk_frame_set_shadow_type(GTK_FRAME(frame[1]), GTK_SHADOW_OUT);

    frame[2] = gtk_frame_new("Shadow Etched In");
    gtk_frame_set_shadow_type(GTK_FRAME(frame[2]), GTK_SHADOW_ETCHED_IN);

    frame[3] = gtk_frame_new("Shadow Etched Out");
    gtk_frame_set_shadow_type(GTK_FRAME(frame[3]), GTK_SHADOW_ETCHED_OUT);

    for (i = 0; i < 4; ++i) {
        gtk_widget_set_size_request(frame[i], 150, 100);
    }

    gtk_fixed_put(fixed, frame[0], x, y);
    gtk_fixed_put(fixed, frame[1], x+170, y);
    gtk_fixed_put(fixed, frame[2], x, y+110);
    gtk_fixed_put(fixed, frame[3], x+170, y+110);

    y += 2*110+10;
}

void on_checkButton_clicked(GtkWidget *widget, gpointer window)
{
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
        gtk_window_set_title(window, "GtkCheckButton");
    } else {
        gtk_window_set_title(window, "");
    }
}

void checkButton_demo(GtkFixed *fixed, gpointer window)
{
    GtkWidget *checkButton;

    /* check button */
    checkButton = gtk_check_button_new_with_label("改变标题名称");
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkButton), TRUE);
    GTK_WIDGET_UNSET_FLAGS(checkButton, GTK_CAN_FOCUS);   /*取消默认锁定*/
    gtk_fixed_put(GTK_FIXED(fixed), checkButton, x, y);
    y += 40;
    g_signal_connect(G_OBJECT(checkButton), "clicked", G_CALLBACK(on_checkButton_clicked), window);
}

void button_demo(GtkFixed *fixed)
{
    GtkWidget *button;

    /* button */
    button = gtk_button_new_with_label("Quit");
    GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);   /*取消默认锁定*/
    gtk_widget_set_size_request(button, 80, 35);
    gtk_fixed_put(GTK_FIXED(fixed), button, x, y);
    y += 40;
    g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);
}
